﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCT.Entities;
using DCT.Data;

public partial class Admin_LichTuan_CNChiTietLichTuan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            BindCatalog.BindLichTuan(cboLichTuan);
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value), Pager1.CurrentIndex);
            }
        }
    }

    protected void BindData(int IdCategories, int PageNo)
    {
        
        int TotalRows = 0;
        using (ChiTietLichTuan ent_ChiTietLichTuan = DataRepository.ChiTietLichTuanProvider.GetById(IdCategories))
        {
        string ThoiGian = ent_ChiTietLichTuan.ThoiGian;
            string[] arrayStr = ThoiGian.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
            //=>> chỗ này nó sẽ split ra 1 mảng 3 phần tử
            txtGioDi.Text = arrayStr[0];
            txtPhutDi.Text = arrayStr[1];           
            


            txtThu.Text = Convert.ToString(ent_ChiTietLichTuan.Thu);
            cboLichTuan.SelectedValue = ent_ChiTietLichTuan.IdLichTuan.ToString();
            txtNgay.Text = Convert.ToString(ent_ChiTietLichTuan.Ngay);
            //txtThoiGian.Text = Convert.ToString(ent_ChiTietLichTuan.ThoiGian);
            txtNoiDung.Text = Server.HtmlDecode(ent_ChiTietLichTuan.NoiDung);
            txtThanhPhan.Text = ent_ChiTietLichTuan.ThanhPhan;
            txtDiaDiem.Text = ent_ChiTietLichTuan.DiaDiem;
            txtChuTri.Text = ent_ChiTietLichTuan.ChuTri;
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            H_CatalogId.Value = Request.QueryString["id"].ToString();
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            BindData(Convert.ToInt32(H_CatalogId.Value), Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLChiTietLichTuan.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }
    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
                using (ChiTietLichTuan ent_ChiTietLichTuan = new ChiTietLichTuan())
        {
            ent_ChiTietLichTuan.IdLichTuan = Convert.ToInt32(cboLichTuan.SelectedValue);
            ent_ChiTietLichTuan.Thu = Convert.ToInt32(txtThu.Text);
            ent_ChiTietLichTuan.Ngay = Convert.ToDateTime(txtNgay.Text);

            String ThoiGian = String.Format("{0}:{1}", Common.StandardString(txtGioDi.Text), Common.StandardString(txtPhutDi.Text));
            //String thoiGian = string.Concat(txtGioDi.Text,txtPhutDi.Text);
            ent_ChiTietLichTuan.ThoiGian = ThoiGian;
            ent_ChiTietLichTuan.NoiDung = Server.HtmlEncode(txtNoiDung.Text);
            ent_ChiTietLichTuan.ThanhPhan = txtThanhPhan.Text;
            ent_ChiTietLichTuan.DiaDiem = txtDiaDiem.Text;
            ent_ChiTietLichTuan.ChuTri = txtChuTri.Text;
            ent_ChiTietLichTuan.NgayCapNhat = Common.GetServerDateTime();
            ent_ChiTietLichTuan.TrangThai = chkTrangThai.Checked;
            bool updateStatus = false;
            try
            {
                if (ent_ChiTietLichTuan.Id == 0)
                {
                    updateStatus = DataRepository.ChiTietLichTuanProvider.Insert(ent_ChiTietLichTuan);
                }
                else
                {
                    ent_ChiTietLichTuan.Id = Convert.ToInt32(H_CatalogId.Value);
                    ent_ChiTietLichTuan.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.ChiTietLichTuanProvider.Update(ent_ChiTietLichTuan);
                }
                if (!updateStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.TINTUC_CAPNHAT_THANHCONG, true);
                    RedirectTo("~/Admin/LichTuan/QLChiTietLichTuan.aspx");
                }
            }


            catch (Exception ex)
            {
                Common.WriteLog("CNChiTietLichTuan.aspx.cs", "cmdLuu_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
            }
        }
    }

    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "";
        txtChuTri.Text = "";
        txtDiaDiem.Text = "";
        txtNgay.Text = "";
        txtNoiDung.Text = "";
        txtThanhPhan.Text = "";
        //txtThoiGian.Text = "";
        txtThu.Text = "";
        chkTrangThai.Checked = true;

        BindCatalog.BindLichTuan(cboLichTuan);
    }

    //protected void KiemtraThoigian(object sender, EventArgs e)
    //{
    //    //string tmpTxttg = txtThoiGian.Text;
    //    string[] chuoithuong = tmpTxttg.Split(new char[] { 'h' });
    //    string[] chuoihoa = tmpTxttg.Split(new char[] { 'H' });
    //    string kq = "";

    //    if (chuoithuong.Length >= 2)
    //    {
    //        //      if((chuoithuong[0].Length<=2) && "00"<=chuoithuong[0] <="00"){

    //        //      }


    //        //}
    //        foreach (string s in chuoithuong)
    //        {
    //            kq += s;
    //        }
    //    }
    //    else
    //    {
    //        foreach (string s in chuoihoa)
    //        {
    //            kq += s;
    //        }
    //    }

    //    lbKiemTraThoiGian.Text = kq;

    //}
}